package com.XinYun.Library.modules.borrow.Dao;

import cn.hutool.core.lang.Assert;
import cn.hutool.db.StatementUtil;
import com.XinYun.Library.modules.borrow.Entity.books;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class borrowDaoImpl implements borrowDao{

    public void security(String sql) throws SQLException {
        //sql语句·检测，调用了HuTools的Assert
        Assert.notBlank(sql, "sql语句不能为空！");
        sql = sql.trim();
        String tag = new Scanner(sql).next();
        if(tag.equalsIgnoreCase("SELECT")||
                tag.equalsIgnoreCase("UPDATE")||
                tag.equalsIgnoreCase("INSERT")||
                tag.equalsIgnoreCase("DELETE")) {
        } else {
            throw new SQLException("请不要输入CRUD以外语句");
        }
    }
    @Override
    public ResultSet select(String sql, Object obj) throws SQLException, IOException {
        try {
            security(sql);
            Connection connection = DruidUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //若传过来obj不为空，则解析参数集合
            if (obj != null) {
                //对问号传参
                preparedStatement.setObject(1, obj);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet;
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }finally {
            DruidUtil.close();
        }
    }

    @Override
    public int update(String sql, Object obj) throws SQLException, IOException {
        security(sql);
        Connection connection= DruidUtil.getConnection();
        PreparedStatement ps = StatementUtil.prepareStatement(connection,sql);
        if (obj != null){
            //对问号传参
            ps.setObject(1,obj);
        }
        int line = ps.executeUpdate();
        return line;
    }

    @Override
    public String day(String endtime) {
        try {
        Long time = System.currentTimeMillis();  //获取当前时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(time);//注意这里返回的是string类型
        return date;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public List<books> getALL(String sql, Object value)throws SQLException, IOException {
        //处理返回结果
        borrowDao bD=new borrowDaoImpl();
        ResultSet resultSet = bD.select(sql,value);
        books books = null;
        List<books> bookList = new ArrayList<>();
        try{
            while (resultSet.next()){
                books = new books();
                //获取数据库表的内容
                books.setBorrow_Times(resultSet.getInt("borrow_count"));
                books.setReturn_Times(resultSet.getInt("return_count"));
                books.setExceed_Times(resultSet.getInt("exceed_count"));
                books.setIsbanned(resultSet.getInt("isbanned"));
                //列入集合
                bookList.add(books);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            DruidUtil.close(resultSet);
        }
        return bookList;
    }
}
